$(function () {
    //表格
    var attentance_datagrid = $("#attentance_datagrid");
    //弹窗
    var attentance_dialog = $("#attentance_dialog");
    //日历
    var attentance_date = $("#attentance_date");
    attentance_datagrid.datagrid({
        url: "/attentance/list",
        columns: [[
            {field: 'id', title: '考勤编号', width: 100,align: 'center'},
            {field: 'employee', title: '员工姓名', width: 100, align: 'center',formatter: formatterEmployee},
            {field: 'nowDate', title: '有效日期', width: 100,align: 'center',formatter:formatDateboxMonth},
            {field: 'signOn', title: '签到时间', width: 100,align: 'center',formatter:formatDateboxTime},
            {field: 'signOut', title: '签退时间', width: 100,align: 'center',formatter:formatDateboxTime},
            {field: 'onStatus', title: '签到状态', width: 100,align: 'center',formatter:formatterOnStatus},
            {field: 'downStatus', title: '签退状态', width: 100,align: 'center',formatter:formatterDownStatus},
            {field: 'signUp', title: '补签时间', width: 100,align: 'center'}
        ]],
        fit: true,
        fitColumns: true,
        border: false,
        //rownumbers: true,
        singleSelect: true,
        pagination: true,
        toolbar: "#attentance_datagrid_toolbar"
    });

    //把方法绑定到一个对象上
    var cmdObj = {
        //点击刷新页面
        reloadAttentance: function () {
            //先清空在加载
            $("#keyword").textbox("clear");
            $("#beginDate").datebox("clear");
            $("#endDate").datebox("clear");
            $.post("/attentance/list", function (data) {
                attentance_datagrid.datagrid("load", data);
            });
            //attentance_datagrid.datagrid("reload");
        },
        //高级查询
        search_realname:function () {
            //获取查询input里面的值
            var keyword = $("#keyword").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            //调用datagrid重新加载数据,并且带上查询条件 keyword
            attentance_datagrid.datagrid("load",{
                keyword:keyword,
                beginDate:beginDate,
                endDate:endDate
            });
        },
        //签到按钮
        signOnAttentance:function () {
            $.post("/attentance/saveForSignOn",function (data) {
                if(data.success){
                    $.messager.alert("温馨提示","签到成功","info",function () {
                        attentance_datagrid.datagrid("reload");
                    });
                }else{
                    $.messager.alert("温馨提示",data.msg,"error");
                }
            });
        },
        //签退按钮
        signOutAttentance:function () {
            $.post("/attentance/saveForSignOut",function (data) {
                if(data.success){
                    $.messager.alert("温馨提示","签退成功","info",function () {
                        attentance_datagrid.datagrid("reload");
                    });
                }else{
                    $.messager.alert("温馨提示",data.msg,"error");
                }
            });
        },
        //补签按钮
        signUpAttentance:function () {
            attentance_dialog.dialog("open");
            attentance_date.calendar({
                fit:true,
                onSelect:function (data) {
                    $.post("/attentance/saveForSignUp",function (data) {
                        if(data.success){
                            $.messager.alert("温馨提示","补签成功","info",function () {
                                attentance_datagrid.datagrid("reload");
                                attentance_dialog.dialog("close");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                            attentance_dialog.dialog("close");
                        }
                    });
                }
            });
        }
    }
    //按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]() ;
    });
    attentance_dialog.dialog({
        title:"日历",
        width:260,
        height:260,
        closed:true
    });
});

//员工格式化
function formatterEmployee(value,row,index) {
    return value ? value.realName : "";
}
//签到状态格式化
function formatterOnStatus(value,row,index) {
    console.log(value);
    if(value == true){
        return "<font color='green'>正常签到</font>";
    }

    if(value == false){
        return "<font color='red'>迟到</font>";
    }
}
//签退格式化
function formatterDownStatus(value,row,index) {
    if(value == true){
        return "<font color='green'>正常签退</font>";
    }
    if(value == false){
        return "<font color='red'>早退</font>";
    }
    if(value == null){
        return "未签退";
    }

}
//时间格式化
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds()
        // millisecond
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
//时间
function formatDatebox(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    } else {
        dt = new Date(value);
    }
    return dt.format("yyyy-MM-dd hh:mm:ss");
}
//月份
function formatDateboxMonth(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    } else {
        dt = new Date(value);
    }
    return dt.format("yyyy-MM-dd");
}
//时间
function formatDateboxTime(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    } else {
        dt = new Date(value);
    }
    return dt.format("hh:mm:ss");
}